package com.stuffthathappens.concurrency.donut;

import com.stuffthathappens.concurrency.Donut;

import javax.swing.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.awt.*;

/**
 * @author Eric M. Burke
 */
public class GraphicalDonutDemo extends JFrame {
    private final BlockingQueue<Donut> queue =
            new LinkedBlockingQueue<Donut>(10);

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GraphicalDonutDemo().setVisible(true);
            }
        });
    }

    public GraphicalDonutDemo() {
        super("Donut Demo");

        Container cp = getContentPane();
        cp.add(createProducerPanels(), BorderLayout.WEST);
        cp.add(new QueueSizePanel(queue), BorderLayout.CENTER);
        cp.add(createConsumerPanels(), BorderLayout.EAST);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    private Component createProducerPanels() {
        JPanel inner = new JPanel(new GridLayout(0,1));
        inner.add(new DonutProducerPanel("Krispy Kreme", queue));
        inner.add(new DonutProducerPanel("Heaven Scent", queue));
        inner.add(new DonutProducerPanel("Schnucks", queue));

        JPanel outer = new JPanel(new BorderLayout());
        outer.add(inner, BorderLayout.NORTH);
        return outer;
    }

    private Component createConsumerPanels() {
        JPanel inner = new JPanel(new GridLayout(0,1));
        inner.add(new DonutConsumerPanel("Barney", queue));
        inner.add(new DonutConsumerPanel("Homer", queue));
        inner.add(new DonutConsumerPanel("Otto", queue));

        JPanel outer = new JPanel(new BorderLayout());
        outer.add(inner, BorderLayout.NORTH);
        return outer;
    }
}
